import Pages from '../../../pages/pages.vue'
import ImportCode from '../import-code/import-code.vue'
import ExportCode from '../export-code/export-code.vue'
import CodeExcel from '../code-excel/code-excel.vue'
import { showMsg, prizeCodeList, setMenuType, codeRemoveBatch } from '../../../../vuex/action'

export default {
  name: 'code-list',
  data() {
    return {
      pages: {
        start: 0,
        totalPages: 0,
        limit: 20
      },
      codeList: [],
      hasUsed: -1,
      totalNum: null,
      notUsedNum: null,
      showCodeExcel: false,
      showExport: false,
      pId: '',
      pName: '',
      aId: '',
      aName: ''
    }
  },
  components: {
    'vue-import-code': ImportCode,
    'vue-export-code': ExportCode,
    'vue-code-excel': CodeExcel,
    'vue-pages': Pages
  },
  vuex: {
    getters: {
      fresh: state => state.miniCodeListNeedFresh,
      activityId: state => state.activityStoreId,
      activityName: state => state.activityStoreName,
      prizeName: state => state.prizeStoreName,
      prizeId: state => state.prizeStoreId
    },
    actions: {
      xShowMsg: showMsg,
      xPrizeCodeList: prizeCodeList,
      xSetMenuType: setMenuType,
      xCodeRemoveBatch: codeRemoveBatch
    }
  },
  ready() {
    this.saveData()
    this.xSetMenuType('activity')
    this.getList({})
  },
  methods: {
    saveData() {
      if (this.prizeId) {
        sessionStorage.setItem('activityId', this.activityId)
        sessionStorage.setItem('activityName', this.activityName)
        sessionStorage.setItem('prizeName', this.prizeName)
        sessionStorage.setItem('prizeId', this.prizeId)
        this.aId = this.activityId
        this.aName = this.activityName
        this.pId = this.prizeId
        this.pName = this.prizeName
      } else {
        this.aId = window.sessionStorage.getItem('activityId')
        this.aName = window.sessionStorage.getItem('activityName')
        this.pName = window.sessionStorage.getItem('prizeName')
        this.pId = window.sessionStorage.getItem('prizeId')
      }
    },
    getList({start=this.pages.start, limit=this.pages.limit}) {
      let id = +this.pId
      this.xPrizeCodeList({
        id: id,
        start: start,
        limit: limit,
        hasUsed: this.hasUsed
      })
        .then((data) => {
          this.codeList = data.list
          this.totalNum = data.totalcount
          this.notUsedNum = data.notusedcount
          this.pages.totalPages = Math.ceil(data.totalcount / limit)
        })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    },
    backActivity() {
      this.$route.router.go('/prize/' + this.pId)
    },
    removeCode(codeId) {
      this.xCodeRemoveBatch({
        id: +codeId
      })
        .then(() => {
        })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    }
  },
  watch: {
    fresh() {
      if (this.fresh) {
        this.getList({})
      }
    }
  }
}
